package oocl.ita.eordering.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import oocl.ita.eordering.po.Client;
import oocl.ita.eordering.po.Food;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.service.ClientService;
import oocl.ita.eordering.service.FoodService;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.vo.Vfood;

@Component
public class InitClass {
	
	@Autowired
	FoodService foodService;
	@Autowired
	MerchantService merchantService;
	@Autowired
	ClientService clientService;
	@Autowired
	OrderService orderService;

	public void initData() {
		Merchant merchant = new Merchant("Kinva", "12345678901", "342625199401160576",
				"http://img2.3lian.com/2014/f5/63/7.jpg", "珠海唐家湾", "黄焖鸡米饭",
				"http://s5.tuanimg.com/deal/deal_image/0822/0848/normal/webp-2c40a063-e3fe-4449-aa31-87ee0ceb972e.jpg",
				"123456", 10);
		merchant.setmPassword(BaseUtil.JohnMd(merchant.getmPassword(), "md5"));
		merchantService.addMerchant(merchant);
		Merchant merchant1 = new Merchant("Knife", "13556155710", "342625199401160571",
				"http://img2.3lian.com/2014/f5/63/7.jpg", "广州唐家湾", "黑焖鸡米饭",
				"http://s5.tuanimg.com/deal/deal_image/0822/0848/normal/webp-2c40a063-e3fe-4449-aa31-87ee0ceb972e.jpg",
				"123456", 10);
		merchantService.addMerchant(merchant1);
		Food food = new Food("白切鸡", 1, 22.22, "http://img1.3lian.com/img2011/08/17/s/09.jpg", 1);
		foodService.addFood(food);
		Food food1 = new Food("红切鸡", 2, 33.33, "http://img1.3lian.com/img2011/08/17/s/09.jpg", 1);
		foodService.addFood(food1);

		Client client = new Client("Stark", "12345678901", "南方软件园", "123456");
		clientService.clientRegist(client);
		Client client1 = new Client("Kinva", "12345678902", "南方软件园", "123456");
		clientService.clientRegist(client1);
		Vfood vfood1 = new Vfood(food);
		Vfood vfood2 = new Vfood(food1);
		List<Vfood> vfoods = new ArrayList<>();
		vfoods.add(vfood1);
		vfoods.add(vfood2);
		ObjectMapper mapper = new ObjectMapper();
		try {
			String jsonList = mapper.writeValueAsString(vfoods);
			Order order = new Order(1, 1, jsonList, 1, new Date(), null, null, null);
			orderService.addOrder(order);
			Order order1 = new Order(1, 1, jsonList, 2, new Date(), new Date(), null, null);
			Order order2 = new Order(1, 1, jsonList, 3, new Date(), new Date(), new Date(), null);
			Order order3 = new Order(1, 1, jsonList, 4, new Date(), new Date(), new Date(), null);
			orderService.addOrder(order1);
			orderService.addOrder(order2);
			orderService.addOrder(order3);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
